/*
public class JQDBConvertUtil {

    public  static Map convertDate(Map data, String[]keys){
        if(keys == null || keys.length <=0)return data;

        for(String it:keys){
            if(data.get(it) != null){
                data.replace(it, java.sql.Date.valueOf(data.get(it).toString())); // new java.sql.Date(data.get("START_TIME").toString()));
            }
        }

        return data;
    }
    public  static  Map convertNumber(Map data,String[]keys){
        if(keys == null || keys.length <=0)return data;

        for(String it:keys){
            if(data.get(it) != null){
                data.replace(it, Integer.parseInt(data.get(it).toString())); // new java.sql.Date(data.get("START_TIME").toString()));
            }
        }

        return data;
    }

    public static List<Map> parserQueryArray(Object[] queries){

        List<Map>  result = new ArrayList<>();
        //[["sssss=", 3 ]or ["mmm in ",["ss","bbb"]]] and []

        //constB (("sssss="
        //value:3
        //constB ")
        //or(mmm in
        //vals:["aa","bb"];
        //constE)) and

        parseArray(queries,result);

        if(result.size() <=0)return null;
        return result;
    }

    private static  void parseArray(Object[]queries,List<Map> result){
        if(queries == null || queries.length<=0)return;
        Map start = new HashMap();
        start.put("constB","(");
        result.add(start);
        Integer index =0;
        while(index <queries.length){
            if(queries[index] instanceof  Object[]){
                parseArray((Object[])queries[index],result);
                index++;
                if(index<queries.length){
                    Map middle = new HashMap();
                    if(queries[index] instanceof  Object[]){
                        middle.put("constB","and");            //缺省情况为and
                        result.add(middle);
//                        parseArray((Object[])queries[index],result);
//                        index++;
                    }
                    else{
                        if(queries[index].toString().equalsIgnoreCase("or") ||
                                queries[index].toString().equalsIgnoreCase("and"))
                            middle.put("constB",queries[index]);         //or 或者and
                        result.add(middle);
                        index++;

                    }


                }
            }else {
                Map middle = new HashMap();
                middle.put("constB",queries[index]);
                index++;
                if(index <queries.length){
                    if(queries[index] instanceof  Object[]){
                        middle.put("vals",queries[index]);
                    }
                    else{
                        middle.put("value",queries[index]);
                    }

                    index++;
                }
                result.add(middle);
            }

        }

        Map end = new HashMap();
        end.put("constE",")");
        result.add(end);

    }

    public static List<Map> parserQueries(String queries){

        Object [] arrs = parserQueriestoArray(queries);

        return parserQueryArray(arrs);
    }


    public static Object [] parserQueriestoArray(String queries){
        JSONArray array = JSON.parseArray(queries);
        return jsonArraytoJavaArray(array);

    }


    private static Object [] jsonArraytoJavaArray(JSONArray array){
        Object[] ret = null;
        if(array != null){
            ret = new Object[array.size()];
            for(Integer i =0;i<array.size();i++){
                Object obj = array.get(i);
                if(obj instanceof  JSONArray){
                    ret[i]=jsonArraytoJavaArray((JSONArray)obj);
                }else {
                    ret[i]= obj;
                }
            }
        }
        return ret;
    }

    public  static  Map<String,Object> updateTimeMap(){
        Map<String,Object> assign = new HashMap<>();
        assign.put("UPDATE_TIME","sysdate");
        return  assign;
    }


}



  <select id="query" parameterType="string" resultType="map">
        select <if test="aliasKeys != null">
        <foreach collection="aliasKeys" item="key" index="index" open="" separator=",">${key}</foreach>
        </if>
        <if test="aliasKeys == null">
            *
        </if> from(
        select
        <if test="keys != null"><foreach collection="keys" item="key" index="index" separator=",">${key}</foreach></if>
        <if test="keys == null">*</if>
         from ${tableName}
        <if test="params != null">
            where
            <foreach collection="params" item="item" index="index" separator="  ">
                <if test="item.constB != null">${item.constB}</if>
                <if test="item.value != null">#{item.value}</if>
                <if test="item.vals != null">
                    <foreach collection="item.vals" item="val" open="(" close=")" separator="," >#{val}</foreach>
                </if>
                <if test="item.constE != null">${item.constE}</if>
            </foreach>
        </if>
        <if test="groupBy != null ">
            group by  <foreach collection="groupBy" item="key" index="index" separator=",">${key}</foreach>
        </if>
        <if test="orderBy != null">order by ${orderBy}</if>
        limit #{size} offset #{first}) yy
    </select>




<select id="query2" parameterType="string" resultType="map">
        select <if test="aliasKeys != null">
        <foreach collection="aliasKeys" item="key" index="index" open="" separator=",">${key}</foreach>
        </if>
        <if test="aliasKeys == null">
            *
        </if> from (
        select * from( select <if test="isDistinct"> DISTINCT </if>
        <if test="sKeys != null">
            <foreach collection="sKeys" item="key" index="index" open="" separator=",">${key}</foreach>
         </if>
         <if test="sKeys == null">
            *
        </if>
        from ( select
        <if test="aKeys != null">
            <foreach collection="aKeys" item="key" index="index" separator=",">a.${key}</foreach>
        </if>
        <if test="aKeys == null">
            a.*
        </if>
        <if test="bKeys != null">
            <foreach collection="bKeys" item="key" index="index" open="," separator=",">b.${key} ${key}_B</foreach>
        </if> from ${a} a <if test="joint != null">${joint}</if>  <if test="joint == null">,</if>
        ${b} b
        <choose> <when test="onC!=null and onC != ''"> on ${onC}</when>
                 <when test="onAKey != null and onAKey!='' and onBKey != null and onBKey !=''">on a.${onAKey}=b.${onBKey} </when>
                 <otherwise></otherwise>
        </choose>) de
        <if test="groupBy != null ">
            group by  <foreach collection="groupBy" item="key" index="index" separator=",">${key}</foreach> ) xx
        </if>
        <if test="params != null">
            where
            <foreach collection="params" item="item" index="index" separator="  ">
                <if test="item.constB != null">
                    ${item.constB}
                </if>

                <if test="item.value != null">
                    #{item.value}
                </if>
                <if test="item.vals != null">
                    <foreach collection="item.vals" item="val" open="(" close=")" separator="," >
                        #{val}
                    </foreach>
                </if>
                <if test="item.constE != null">
                    ${item.constE}
                </if>

            </foreach>

        </if>
        <if test="groupBy == null ">
            ) xx
        </if>

        <if test="orderBy != null and orderBy != ''">
            order by ${orderBy}
        </if>
        limit #{size} offset #{first})yy
    </select>




*/

class JQSqlUtil {
    constructor() {
     
    }
	
    //[["sssss=", 3 ]or ["mmm in ",["ss","bbb"]]] and []
     static parserQueryArray(queries){

        let   result =[];
       
        let temp = JQSqlUtil.parseArray(queries,result);
        if(!temp)return null;
        if(result.length <=0)return null;
        return result;
    }

    //queries object[] result map[]
     static  parseArray(queries, result){
		
        if(!queries)return false;
        let start = {};
        start["constB"]="(";
        result.push(start);
        let index =0;
        while(index <queries.length){
            if(Array.isArray(queries[index])){
				
                let temp = JQSqlUtil.parseArray(queries[index],result);
				if(!temp)return false;
                index++;
                if(index<queries.length){
                    let middle = {};
                    if(Array.isArray(queries[index])){
						if(queries[index].length<=0)return false;
                        middle["constB"]="and";            //缺省情况为and
                        result.push(middle);
                    }
                    else{
                        if(queries[index].toString().toLowerCase() ==="or" ||
                                queries[index].toString().toLowerCase() ==="and")
                            middle["constB"]=queries[index];         //or 或者and
                        result.push(middle);
                        index++;

                    }


                }
            }else {
                let middle = {};
                middle["constB"]=queries[index];
                index++;
                if(index <queries.length){
                    if(Array.isArray(queries[index])){
						if(!queries[index].length)return false;
                        middle["vals"]=queries[index];
                    }
                    else{
                        middle["value"]=queries[index];
                    }

                    index++;
                }
                result.push(middle);
            }

        }

        let end = {};
        end["constE"]=")";
        result.push(end);
		return true;

    }
    
	
}
module.exports = JQSqlUtil;